package cn.doitedu.data_mocker;


import com.alibaba.fastjson2.JSON;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class EventsGenerator {

    public static void main(String[] args) throws Exception {

        if(args.length<4){
            System.err.println("参数格式：dt sessionCnt threads path");
            System.exit(1);
        }


        AtomicInteger threadsCounter = new AtomicInteger();

        String dt = args[0];
        int sessionCnt = Integer.parseInt(args[1]);
        int threads = Integer.parseInt(args[2]);
        String path = args[3];

        Date date = DateUtils.parseDate(dt, "yyyy-MM-dd");
        long startTime = date.getTime();


        for (int i = 0; i < threads; i++) {
            new Thread(new GeneratorRunner(dt,startTime, threadsCounter, sessionCnt / threads, threads,i,path)).start();
        }


        while (true) {
            if (threadsCounter.intValue() == threads) {
                System.out.println("全部线程完成");
                break;
            }
            Thread.sleep(2000);
        }


    }


    public static class GeneratorRunner implements Runnable {

        HashMap<String, Object> dataMap = new HashMap<>();
        String[] appIds = {"top.doe.app1", "top.doe.app2"};
        String[] versions = {"v1.0", "v1.2", "v1.4"};
        String[] carriers = {"中国移动", "中国电信", "中国联通", "中国铁通", "中国卫通"};
        String[] resolutions = {"760*1024", "1024*1920", "1080*1920", "360*240", "480*960"};
        String[] channels = {"360应用市场", "小米应用", "豌豆荚", "华为应用市场", "腾讯应用"};
        String[] nets = {"wifi", "4G", "3G", "5G", "有线"};
        String[] osnames = {"android", "mac", "windows", "linux", "sibian"};
        BufferedWriter bw;
        long startTime;
        AtomicInteger threadsCounter;
        int sessionCnt;
        int threads;
        String path;

        public GeneratorRunner(String dt, long startTime, AtomicInteger threadsCounter, int sessionCnt, int threads, int i,String path) throws IOException {
            this.startTime = startTime;
            this.threadsCounter = threadsCounter;
            this.sessionCnt = sessionCnt;
            this.threads = threads;
            this.path = path;

            this.bw = new BufferedWriter(new FileWriter(path+"/log_"+dt+".log"+"."+(i+1)));
        }

        @Override
        public void run() {
            int step = 0;
            int stepJump = sessionCnt / 24;
            int ses = 0;

            long maxTime = startTime + 23 * 60 * 60 * 1000 + 50 * 60 * 1000;


            while (startTime < maxTime && (ses++) <= sessionCnt) {
                dataMap.put("username", RandomStringUtils.randomAlphabetic(3));
                dataMap.put("app_id", appIds[RandomUtils.nextInt(0, 2)]);
                dataMap.put("app_version", versions[RandomUtils.nextInt(0, 3)]);
                dataMap.put("release_channel", channels[RandomUtils.nextInt(0, 5)]);
                dataMap.put("carrier", carriers[RandomUtils.nextInt(0, 5)]);
                dataMap.put("net_type", nets[RandomUtils.nextInt(0, 5)]);
                dataMap.put("ip", RandomUtils.nextInt(100, 130) + "." + RandomUtils.nextInt(20, 60) + "." + RandomUtils.nextInt(0, 10) + "." + RandomUtils.nextInt(100, 200));
                dataMap.put("device_id", RandomStringUtils.randomAlphanumeric(8).toLowerCase());
                dataMap.put("device_type", RandomStringUtils.randomAlphanumeric(3).toLowerCase());
                dataMap.put("resolution", resolutions[RandomUtils.nextInt(0, 5)]);
                dataMap.put("os_name", osnames[RandomUtils.nextInt(0, 5)]);
                dataMap.put("os_version", "v" + RandomUtils.nextInt(1, 3) + "." + RandomUtils.nextInt(1, 5));
                dataMap.put("latitude", RandomUtils.nextDouble(30.0, 50.0));
                dataMap.put("longitude", RandomUtils.nextDouble(80.0, 130.0));


                HashMap<String, String> properties = new HashMap<>();
                properties.put("url", "/" + RandomStringUtils.randomAlphanumeric(2) + "/" + RandomStringUtils.randomAlphanumeric(2));
                dataMap.put("properties", properties);

                dataMap.put("session_id", RandomStringUtils.randomAlphabetic(8).toUpperCase());

                long action_time = startTime + RandomUtils.nextInt(1000, 30000);

                for (int i = 0; i < RandomUtils.nextInt(5, 100) && (action_time += RandomUtils.nextInt(1000, 30000)) < maxTime ; i++) {
                    dataMap.put("event_id", "e_" + RandomUtils.nextInt(1, 50));
                    dataMap.put("action_time", action_time);

                    properties.put("url", "/" + RandomStringUtils.randomAlphanumeric(2) + "/" + RandomStringUtils.randomAlphanumeric(2));

                    String jsonString = JSON.toJSONString(dataMap);
                    //System.out.println(jsonString);
                    try {
                        bw.write(jsonString);
                        bw.newLine();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }

                step++;

                if (step >= stepJump) {
                    startTime += 60 * 60 * 1000;
                    step = 0;
                }

            }

            int i = threadsCounter.incrementAndGet();


            try {
                bw.flush();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("线程完成,此刻完成数: " + i);

        }
    }


}
